package com.tjp.eight.ui.fragment.subdetails;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tjp.eight.R;
import com.tjp.eight.utils.BitmapUtils;
import com.tjp.eight.utils.StringUtils;
import com.tjp.eight.views.CircleImageView;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/2/27.
 */
public class SubDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public SubDetailAdapter(Context context) {
        this.context = context;
    }

    private SubDetailResultModel model;
    private EnterDetails enterDetails;
    private Context context;
    private SubListener subListener;
    private CircleImageView curCiv;
    private final int HEAD = 0;
    private final int NORMAL = 1;
    private final int BIG = 2;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case HEAD:
                View head = LayoutInflater.from(context).inflate(R.layout.item_sub_detail_head, parent, false);
                return new SubDetailHeadViewHolder(head);
            case NORMAL:
                View normal = LayoutInflater.from(context).inflate(R.layout.item_sub_detail, parent, false);
                return new SubDetailNorViewHolder(normal);
        }
        View big = LayoutInflater.from(context).inflate(R.layout.item_main_large, parent, false);
        return new SubDetailBigViewHolder(big);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case HEAD:
                ((SubDetailHeadViewHolder) holder).tv_sub_views.setText("" + StringUtils.toWan(model.subscibe));
                ((SubDetailHeadViewHolder) holder).tv_title_cat.setText("" + model.name);
                Glide.with(context).load(model.cover_path).error(R.drawable.loading).into(((SubDetailHeadViewHolder) holder).iv_title_pic);
                ((SubDetailHeadViewHolder) holder).civ_is_sub.setImageBitmap(model.if_subscibe == 1 ?
                        BitmapUtils.getBitmap(context, R.drawable.subscribed_big) : BitmapUtils.getBitmap(context, R.drawable.un_subscribed_big));
                ((SubDetailHeadViewHolder) holder).civ_is_sub.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //点击订阅或者取消订阅
                        curCiv = ((SubDetailHeadViewHolder) holder).civ_is_sub;
                        if (subListener != null) {
                            subListener.sub(model.caid, model.name, model.cover_path);
                        }
                    }
                });
                break;
            case NORMAL:
                final int pos = position - 1;
                ((SubDetailNorViewHolder) holder).tv_sub_views.setText("" + StringUtils.toWan(model.data.get(pos).views_all));
                ((SubDetailNorViewHolder) holder).tv_title.setText("" + model.data.get(pos).title);
                Glide.with(context).load(model.data.get(pos).cover_path).error(R.drawable.loading).into(((SubDetailNorViewHolder) holder).iv_sub_details);
                ((SubDetailNorViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //进入文章详情
                        if (enterDetails != null) {
                            enterDetails.enterDetail(model.data.get(pos).cid);
                        }
                    }
                });
                break;
            case BIG:
                final int pos1 = position - 1;
                ((SubDetailBigViewHolder) holder).tv_sub_views.setText("" + StringUtils.toWan(model.data.get(pos1).views_all));
                ((SubDetailBigViewHolder) holder).tv_title.setText("" + model.data.get(pos1).title);
                Glide.with(context).load(model.data.get(pos1).cover_path).error(R.drawable.loading).into(((SubDetailBigViewHolder) holder).iv_sub_details);
                ((SubDetailBigViewHolder) holder).view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //进入文章详情
                        if (enterDetails != null) {
                            enterDetails.enterDetail(model.data.get(pos1).cid);
                        }
                    }
                });
                break;
        }
    }


    public void setEnterDetails(EnterDetails enterDetails) {
        this.enterDetails = enterDetails;
    }

    public void setSubListener(SubListener subListener) {
        this.subListener = subListener;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HEAD;
        }
        if (position % 4 == 0)
            return BIG;
        return NORMAL;
    }

    @Override
    public int getItemCount() {
        return model == null || model.data == null ? 0 : model.data.size() + 1;
    }

    public void addData(SubDetailResultModel model) {
        this.model = model;
        notifyDataSetChanged();
    }

    public void addMoreData(ArrayList<SubDetailsItemModel> data) {
        if (data == null || data.isEmpty()) return;
        if (model == null) return;
        if (model.data == null) {
            model.data = data;
        } else {
            for (SubDetailsItemModel m : data) {
                model.data.add(m);
            }
        }
        notifyItemChanged(getItemCount());
        notifyDataSetChanged();
    }

    public void notifyIsSub(int yes) {
        model.if_subscibe = yes;
        if (curCiv != null) {
            curCiv.setImageBitmap(model.if_subscibe == 1 ?
                    BitmapUtils.getBitmap(context, R.drawable.subscribed_big) : BitmapUtils.getBitmap(context, R.drawable.un_subscribed_big));
        }
    }

    private class SubDetailHeadViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_title_pic;
        CircleImageView civ_is_sub;
        TextView tv_title_cat;
        TextView tv_sub_views;

        public SubDetailHeadViewHolder(View view) {
            super(view);
            iv_title_pic = (ImageView) view.findViewById(R.id.iv_title_pic);
            civ_is_sub = (CircleImageView) view.findViewById(R.id.civ_is_sub);
            tv_title_cat = (TextView) view.findViewById(R.id.tv_title_cat);
            tv_sub_views = (TextView) view.findViewById(R.id.tv_sub_views);

        }
    }

    private class SubDetailNorViewHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        TextView tv_sub_views;
        ImageView iv_sub_details;
        View view;

        public SubDetailNorViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_sub_views = (TextView) itemView.findViewById(R.id.tv_sub_views);
            iv_sub_details = (ImageView) itemView.findViewById(R.id.iv_sub_details);

        }
    }

    private class SubDetailBigViewHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        TextView tv_sub_views;
        TextView tv_show_tips;
        ImageView iv_sub_details;
        View view;

        public SubDetailBigViewHolder(View view) {
            super(view);
            this.view = view;
            tv_title = (TextView) view.findViewById(R.id.tv_show_title);
            tv_show_tips = (TextView) view.findViewById(R.id.tv_show_tips);
            tv_show_tips.setVisibility(View.GONE);
            tv_sub_views = (TextView) view.findViewById(R.id.tv_show_read);
            iv_sub_details = (ImageView) view.findViewById(R.id.iv_pic);

        }
    }

    public interface EnterDetails {
        void enterDetail(String cid);
    }

    public interface SubListener {
        void sub(String caid, String name, String cover_path);
    }
}
